package hk.ust.android_competition.view_adapter;

import android.graphics.Point;
import android.view.Display;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import hk.ust.android_competition.activities.MiniGameActivity;
import hk.ust.android_competition.e_pet.R;
import hk.ust.android_competition.event.MiniGame;
import hk.ust.android_competition.pet.PetType;

/**
 * Created by Silver on 18/1/15.
 */
public class MiniGameViewAdapter extends ViewAdapter {
    private ImageView petImage;
    private ImageView position0;
    private ImageView position1;
    private ImageView position2;
    private ImageView position3;
    private ImageView position4;
    private ImageView position5;
    private ImageView position6;
    private ImageView position7;
    private ImageView position8;
    private TextView tv_status;
    private Button back;

    private ArrayList<ImageView> imageViews = new ArrayList<ImageView>();


    private final int playerChoseImageSource = R.drawable.player_chose;
    private final int computerChoseImageSource = R.drawable.computer_chose;

    public MiniGameViewAdapter(MiniGameActivity activity, int contentViewId) {
        super(activity, contentViewId);
        initialize();
    }

    private void initialize() {
        imageViewsFormList(position0, R.id.position0);
        imageViewsFormList(position1, R.id.position1);
        imageViewsFormList(position2, R.id.position2);
        imageViewsFormList(position3, R.id.position3);
        imageViewsFormList(position4, R.id.position4);
        imageViewsFormList(position5, R.id.position5);
        imageViewsFormList(position6, R.id.position6);
        imageViewsFormList(position7, R.id.position7);
        imageViewsFormList(position8, R.id.position8);
        petImage = (ImageView) getActivity().findViewById(R.id.petImage);
        tv_status = (TextView) getActivity().findViewById(R.id.tv_status);
        back = (Button) getActivity().findViewById(R.id.game_back);
        back.setOnClickListener(getActivity());
    }

    private void imageViewsFormList(ImageView position, int imageViewId) {
        position = (ImageView) getActivity().findViewById(imageViewId);
        position.setOnClickListener(getActivity());
        imageViews.add(position);
    }

    public void setPetImage(PetType petType) {
        switch (petType) {
            case Panda:
                petImage.setImageResource(pet_image_source[0][0]);
                break;
            case Rabbit:
                petImage.setImageResource(pet_image_source[1][0]);
                break;
            case Giraffe:
                petImage.setImageResource(pet_image_source[2][0]);
                break;
        }

    }

    public void updateGameLayout(final MiniGame game) {
        for (int i = 0; i < imageViews.size(); i++) {
            if (game.isComputerChose(i)) {
                imageViews.get(i).setImageResource(computerChoseImageSource);
                imageViews.get(i).setClickable(false);
            } else if (game.isPlayerChose(i)) {
                imageViews.get(i).setImageResource(playerChoseImageSource);
                imageViews.get(i).setClickable(false);
            }
        }
        if (game.isEnd())
            if (game.isComputerWin())
                tv_status.setText("Pet wins!");
            else if (game.isPlayerWin())
                tv_status.setText("Player wins!");
            else
                tv_status.setText("Draw!");
        else
            tv_status.setText("Player's turn");
    }

}
